
using System.Collections;
using System.Collections.Generic;
using System.Data;
using AppGameState;
using UnityEngine;
using AppTableUtils;

using UnityEngine.Networking;


public enum GameEndingTypeEnum
{
    None,
    Win,
    Lose,
    Draw
}


public class BaseInfoManager : MonoBehaviour
{
    public static BaseInfoManager instance;

    [HideInInspector] public bool is_ready = false;

    void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    void init()
    {
        BdUtils.call(card_dt);
        is_ready = true;
    }
    
    public Transform message_parent_canvas;

    [Header("当前金币")] public int current_coins = 5;
    [Header("最大金币")] public int MAX_COINS = 10;
    [Header("刷新消耗")] public int cost_of_refresh_store = 1;
    [Header("售出随从")] public int price_of_sale_soldier = 1;

    public void sale_soldier()
    {
        current_coins += price_of_sale_soldier;
    }

    [HideInInspector] public int current_rounds = 0;  // 游戏回合数
    
    [HideInInspector] public int remain_enemies = 5;
    public int current_store_level = 1;
    
    [HideInInspector] public const int MAX_STORE_LEVEN = 6;

    public bool frozen_store_cards = false;

    [HideInInspector] public int MAX_BUFF_STACK = 9999;

    public string path_of_card_dt = "card_data";

    [HideInInspector] public int MAX_STAR_LEVEL = 7;
    
    private DataTable _card_dt;
    public DataTable card_dt
    {
        get
        {
            if (_card_dt == null)
            {
                if (BdTools.Flags.is_webgl)
                {
                    string new_f_name = TableUtils.change_file_extension(path_of_card_dt, ".csv");
                    var new_file_path = BdUtils.join(BdUtils.streaming_data_dir_path, new_f_name);
                    // new_file_path = BdUtils.get_relative_sub_path(BdUtils.streaming_data_dir_path, new_file_path);
                    StartCoroutine(load_streaming_file_by_web_request(new_file_path, result =>
                    {
                        _card_dt = TableUtils.load_dt_from_str(result.file_content, primary_key: "ID");
                    }));
                }
                else
                {
                    _card_dt = TableUtils.read_table(path_of_card_dt, primary_key: "ID");
                }
            }
            return _card_dt;
        }
    }

    // IEnumerator read_table_by_web_request(string file_path,  System.Action<LoadResult> callback)
    // {
    // }
    
    public class LoadResult
    {
        public string file_url { get; set; }
        public string file_path { get; set; }
        public string file_content { get; set; }
    }
    
    IEnumerator load_streaming_file_by_web_request(string file_path,  System.Action<LoadResult> callback)
    {
        // 构造文件路径
        // string file_url = Application.streamingAssetsPath + '/' + file_path;
        string file_url = file_path;
        if (!file_path.StartsWith(Application.streamingAssetsPath))
            file_url = BdUtils.join(BdUtils.streaming_data_dir_path, file_path);
        
        // Debug.Log("--- load_streaming_file_by_web_request.file_url: \n" + file_url);

        // 使用 UnityWebRequest 读取文件
        UnityWebRequest request = UnityWebRequest.Get(file_url);
        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.Success)
        {
            string csvData = request.downloadHandler.text;
            // Debug.Log("--- CSV 文件内容 csvData: \n" + csvData);
            
            // 创建结果并通过回调返回
            var result = new LoadResult
            {
                file_url = file_url,
                file_path = file_path,
                file_content = csvData
            };
            callback?.Invoke(result);
        }
        else
        {
            BdPackage.BdUtils.log("*** file_path:", file_path, ", 加载 CSV 文件失败: " + request.error);
        }
    }

    void Start()
    {
        init();
    }

    void Update()
    {
    }
}
